package net.ocps.tchs.soap;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.StringTokenizer;

import net.ocps.tchs.soap.entity.DrySoap;
import net.ocps.tchs.soap.entity.JumpSlime;
import net.ocps.tchs.soap.entity.Player;
import net.ocps.tchs.soap.entity.Slime;
import net.ocps.tchs.soap.object.Platform;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.math.Rectangle;

public class Utils  {

	public static boolean collides( Rectangle thisone, Rectangle thatone ) {
		if( thisone.overlaps( thatone ) ) {
			return true;
		}
		return false;
	}
	
	public static void loadMap( String map ) {
		try {
			Main.currentMap = map;
			FileHandle handle = Gdx.files.internal("data/maps/"+map);  
			//read = new FileReader( handle.file() );
			BufferedReader buff = new BufferedReader( handle.reader() );
			ArrayList<String> things = new ArrayList<String>();
			String temp = "n";
			while( temp != null ) {
				temp = buff.readLine();
				if( temp != null ) {
					System.out.println("Added data: "+temp);
					things.add( temp );
				}
			}
			System.out.println("Done reading["+things.size()+"], loading Entitys...");
			for( int i = 0; i < things.size(); i++ ) {
				splitter = new StringTokenizer( things.get( i ), "," );
				String name = splitter.nextToken();
				if( name.equalsIgnoreCase("Player") ) {
					System.out.print("Loading Player...");
					Main.entitys.add( new Player(
							Integer.parseInt(splitter.nextToken()),
							-1*Integer.parseInt(splitter.nextToken()),
							Integer.parseInt(splitter.nextToken()),
							Integer.parseInt(splitter.nextToken()),
							splitter.nextToken()));
					System.out.print(" Done.\n");
				} else if( name.equalsIgnoreCase("Slime") ) {
					System.out.print("Loading Slime...");
					Main.entitys.add( new Slime(
							Integer.parseInt(splitter.nextToken()),
							-1*Integer.parseInt(splitter.nextToken()),
							Integer.parseInt(splitter.nextToken()),
							Integer.parseInt(splitter.nextToken()),
							splitter.nextToken()));
					System.out.print(" Done.\n");
				} else if( name.equalsIgnoreCase("Jump Slime") ) {
					System.out.print("Loading Slime...");
					Main.entitys.add( new JumpSlime(
							Integer.parseInt(splitter.nextToken()),
							-1*Integer.parseInt(splitter.nextToken()),
							Integer.parseInt(splitter.nextToken()),
							Integer.parseInt(splitter.nextToken()),
							splitter.nextToken()));
					System.out.print(" Done.\n");
				} else if( name.equalsIgnoreCase("Platform") ) {
					System.out.print("Loading Platform...");
					Main.staticobjects.add( new Platform(
							Integer.parseInt(splitter.nextToken()),
							-1*Integer.parseInt(splitter.nextToken()),
							Integer.parseInt(splitter.nextToken()),
							Integer.parseInt(splitter.nextToken()),
							splitter.nextToken()));
					System.out.print(" Done.\n");
				} else if( name.equalsIgnoreCase("DrySoap") || splitter.nextToken().equalsIgnoreCase("Dry Soap") ) {
					System.out.print("Loading Dry Soap...");
					Main.entitys.add( new DrySoap(
							Integer.parseInt(splitter.nextToken()),
							-1*Integer.parseInt(splitter.nextToken()),
							Integer.parseInt(splitter.nextToken()),
							Integer.parseInt(splitter.nextToken()),
							splitter.nextToken()));
					System.out.print(" Done.\n");
				}
			}
		} catch ( Exception e ) {
			e.printStackTrace();
		}
		
	}
	
	public static StringTokenizer splitter;
	public static FileReader read;
}
